package com.ruoyi.crm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.CrmTask;
import com.ruoyi.crm.service.ICrmTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单列表Controller
 * 
 * @author 最帅的陈彬彬
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/crm/task")
public class CrmTaskController extends BaseController
{
    @Autowired
    private ICrmTaskService crmTaskService;

    /**
     * 查询工单列表列表
     */
    @PreAuthorize("@ss.hasPermi('crm:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmTask crmTask) {
        startPage();
        List<CrmTask> list = crmTaskService.selectCrmTaskList(crmTask);
        return getDataTable(list);
    }

    /**
     * 导出工单列表列表
     */
    @PreAuthorize("@ss.hasPermi('crm:task:export')")
    @Log(title = "工单列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmTask crmTask)
    {
        List<CrmTask> list = crmTaskService.selectCrmTaskList(crmTask);
        ExcelUtil<CrmTask> util = new ExcelUtil<CrmTask>(CrmTask.class);
        util.exportExcel(response, list, "工单列表数据");
    }

    /**
     * 获取工单列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(crmTaskService.selectCrmTaskById(id));
    }

    /**
     * 新增工单列表
     */
    @PreAuthorize("@ss.hasPermi('crm:task:add')")
    @Log(title = "工单列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmTask crmTask)
    {
        return toAjax(crmTaskService.insertCrmTask(crmTask));
    }

    /**
     * 完成工单列表关联工作流
     */

    @PostMapping(value = "/accomplishTaskInfo")
    public AjaxResult accomplishTaskInfo(@RequestBody CrmTask crmTask) {
        return toAjax(crmTaskService.accomplishTaskInfo(crmTask));
    }

    /**
     * 修改工单列表
     */
    @PreAuthorize("@ss.hasPermi('crm:task:edit')")
    @Log(title = "工单列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmTask crmTask)
    {
        return toAjax(crmTaskService.updateCrmTask(crmTask));
    }

    /**
     * 删除工单列表
     */
    @PreAuthorize("@ss.hasPermi('crm:task:remove')")
    @Log(title = "工单列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(crmTaskService.deleteCrmTaskByIds(ids));
    }

    /**
     * 审核
     *
     * @param crmTask
     * @return
     */
    @PostMapping("audit")
    public AjaxResult audit(@RequestBody CrmTask crmTask) {
        return AjaxResult.success(crmTaskService.audit(crmTask));
    }
}
